home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / dyndlg.zip / BASE.C next >
C/C++ Source or Header  |  1992-11-02  |  11KB  |  448 lines

  1. /**************************************************************************\
  2. *  Simple sample to show how to dynamically create Win32 dialogs.
  3. *
  4. *         Steve Firebaugh
  5. *         Microsoft Developer Support
  6. *         Copyright (c) 1992 Microsoft Corporation
  7. *
  8. *
  9. *  Post questions or folloups to Compuserve MSWIN32, section 4,
  10. *   attn:  Steve Firebaugh
  11. *
  12. *  For more information on the resource formats in Win32 look for the
  13. *   RESFMT.ZIP file.
  14. *
  15. \**************************************************************************/
  16.  
  17. #define UNICODE
  18. #include <windows.h>
  19.  
  20. LRESULT APIENTRY MainWndProc(HWND, UINT, UINT, LONG);
  21. LRESULT APIENTRY About(HWND, UINT, WPARAM, LPARAM );
  22.  
  23. /**************************************************************************\
  24. *
  25. *  function:  WinMain()
  26. *
  27. *  input parameters:  c.f. generic sample
  28. *
  29. \**************************************************************************/
  30. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  31.                      LPSTR lpCmdLine, int nCmdShow)
  32. {
  33.     HWND  hwnd;
  34.     MSG   msg;
  35.  
  36.     UNREFERENCED_PARAMETER( lpCmdLine );
  37.  
  38.  
  39.     /* Check for previous instance.  If none, then register class. */
  40.     if (!hPrevInstance) {
  41.         WNDCLASS  wc;
  42.  
  43.         wc.style = NULL;
  44.         wc.lpfnWndProc = (WNDPROC)MainWndProc;
  45.  
  46.         wc.cbClsExtra = 0;
  47.         wc.cbWndExtra = 0;
  48.         wc.hInstance = hInstance;
  49.         wc.hIcon = NULL;
  50.         wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  51.         wc.hbrBackground = GetStockObject(LTGRAY_BRUSH);
  52.         wc.lpszMenuName =  NULL;
  53.         wc.lpszClassName = TEXT("base");
  54.  
  55.         if (!RegisterClass(&wc)) return (FALSE);
  56.     }  /* class registered o.k. */
  57.  
  58.  
  59.     /* Create the main window.  Return false if CreateWindow() fails */
  60.     hwnd = CreateWindow(
  61.         TEXT("base"),
  62.         TEXT("base"),
  63.         WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_VISIBLE,
  64.         CW_USEDEFAULT,
  65.         CW_USEDEFAULT,
  66.         CW_USEDEFAULT,
  67.         CW_USEDEFAULT,
  68.         NULL,
  69.         NULL,
  70.         hInstance,
  71.         NULL);
  72.  
  73.     if (!hwnd) return (FALSE);
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84. /*+++
  85.  
  86.  
  87.     Create the first dialog dynamically.  Notice that we are NOT using
  88.     structures here because too many of the fields are of variable length.
  89.     Instead, just allocate some memory to play with, and start filling in
  90.     the data at that pointer.
  91.  
  92.     p - pointer which is moved down through the DLGTEMPLATE information.
  93.     pdlgtemplate - pointer to the TOP of the DLGTEMPLATE information.
  94.  
  95.  
  96.     Notice that UNICODE is defined to be on in this module.  That means:
  97.       1.  All strings included in TEXT() macro will be made unicode strings
  98.         by the compiler.
  99.       2.  wsprintf() will accept a unicode string as input, and will fill
  100.         its lpOut buffer with a unicode string.  Notice that in any case,
  101.         the return value is the number of *characters*  not the number of
  102.         bytes.
  103.       3.  Any system call which may be dependent upon unicode will be mapped
  104.         to its wide character version (*W not *A) by the header files.
  105.         Howard Myers has pointed out that this does not matter for the
  106.         CreateDialogIndirect() call.  Both the A and W versions expect the
  107.         dialog template to contain wide character strings.
  108.  
  109.  
  110.     Here we create a simple dialog with one item.  The dialog has a title,
  111.     the item has text, and the item class is specified by ordinal.  There
  112.     is no font information.
  113.  
  114.  
  115. ---*/
  116.  
  117. {
  118. WORD *p, *pdlgtemplate;
  119. int nchar;
  120.  
  121.   /* declare variables purely for ease of reading the names provide. */
  122.   DWORD   lStyle;
  123.   DWORD   lExtendedStyle;
  124.   WORD    NumberOfItems;
  125.   WORD    x;
  126.   WORD    y;
  127.   WORD    cx;
  128.   WORD    cy;
  129.  
  130.   WORD    wId;
  131.  
  132.   /* allocate some memory to play with  */
  133.   pdlgtemplate = p = (PWORD) LocalAlloc (LPTR, 1000);
  134.  
  135.   lStyle              = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | WS_VISIBLE;
  136.   lExtendedStyle      = 0;
  137.   NumberOfItems       = 1;
  138.   x                   = 10;
  139.   y                   = 10;
  140.   cx                  = 100;
  141.   cy                  = 100;
  142.  
  143.   /* start to fill in the dlgtemplate information.  addressing by WORDs */
  144.   *p++ = LOWORD (lStyle);
  145.   *p++ = HIWORD (lStyle);
  146.   *p++ = LOWORD (lExtendedStyle);
  147.   *p++ = HIWORD (lExtendedStyle);
  148.   *p++ = NumberOfItems;
  149.   *p++ = x ;
  150.   *p++ = y ;
  151.   *p++ = cx;
  152.   *p++ = cy;
  153.   *p++ = 0;     // Menu
  154.   *p++ = 0;     // Class
  155.  
  156.   /* copy the title of the dialog, null terminate the string. */
  157.   nchar = wsprintf (p, TEXT("Title 1"));
  158.   p += nchar;
  159.   *p++ = 0;
  160.  
  161.   /* add in the wPointSize and szFontName here iff the DS_SETFONT bit on */
  162.  
  163.   /* make sure the first item starts on a DWORD boundary */
  164.   { ULONG l;
  165.  
  166.   l = (ULONG) p;
  167.   l +=3;
  168.   l >>=2;
  169.   l <<=2;
  170.   p = (PWORD) l;
  171.   }
  172.  
  173.  
  174.   /* now start with the first item */
  175.   lStyle              = BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD;
  176.   x                   = 10;
  177.   y                   = 70;
  178.   cx                  = 80;
  179.   cy                  = 10;
  180.   wId                 = IDOK;
  181.  
  182.  
  183.   *p++ = LOWORD (lStyle);
  184.   *p++ = HIWORD (lStyle);
  185.   *p++ = LOWORD (lExtendedStyle);
  186.   *p++ = HIWORD (lExtendedStyle);
  187.   *p++ = x ;
  188.   *p++ = y ;
  189.   *p++ = cx;
  190.   *p++ = cy;
  191.   *p++ = wId;
  192.  
  193.   /* fill in class i.d. Button in this case */
  194.   *p++ = (WORD)0xffff;
  195.   *p++ = (WORD)0x0080;
  196.  
  197.   /* copy the text of the first item, null terminate the string. */
  198.   nchar = wsprintf (p, TEXT("OK"));
  199.   p += nchar;
  200.   *p++ = 0;
  201.  
  202.  
  203.   CreateDialogIndirect (hInstance, (LPDLGTEMPLATE) pdlgtemplate, hwnd, (DLGPROC) About);
  204.  
  205.   LocalFree (LocalHandle (pdlgtemplate));
  206.  
  207. }
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218. /*+++
  219.  
  220.  
  221.     Create the second dialog dynamically.
  222.  
  223.     Here we create a dialog which has font information (DS_SETFONT),
  224.     and which has two items with the item class specified by name.
  225.  
  226.     BUG BUG.  Notice that there is an outstanding, peculiar bug in
  227.     the October beta release which affects dialogs that set their
  228.     font.  When the application is run for the second time, the
  229.     font information is screwed up, and the edit fields do not work
  230.     correctly.  ?????  This is a known bug, and a fix is in progress
  231.     for the next release.
  232.  
  233.  
  234. ---*/
  235.  
  236. {
  237. WORD *p, *pdlgtemplate;
  238. int nchar;
  239.  
  240.   /* declare variables purely for ease of reading the names provide. */
  241.   DWORD   lStyle;
  242.   DWORD   lExtendedStyle;
  243.   WORD    NumberOfItems;
  244.   WORD    x;
  245.   WORD    y;
  246.   WORD    cx;
  247.   WORD    cy;
  248.  
  249.   WORD    wId;
  250.  
  251.   /* allocate some memory to play with  */
  252.   pdlgtemplate = p = (PWORD) LocalAlloc (LPTR, 1000);
  253.  
  254.   lStyle              = DS_SETFONT | WS_CAPTION | WS_SYSMENU | WS_VISIBLE;
  255.   lExtendedStyle      = 0;
  256.   NumberOfItems       = 2;
  257.   x                   = 210;
  258.   y                   = 10;
  259.   cx                  = 100;
  260.   cy                  = 100;
  261.  
  262.   /* start to fill in the dlgtemplate information.  addressing by WORDs */
  263.   *p++ = LOWORD (lStyle);
  264.   *p++ = HIWORD (lStyle);
  265.   *p++ = LOWORD (lExtendedStyle);
  266.   *p++ = HIWORD (lExtendedStyle);
  267.   *p++ = NumberOfItems;
  268.   *p++ = x ;
  269.   *p++ = y ;
  270.   *p++ = cx;
  271.   *p++ = cy;
  272.   *p++ = 0;     // Menu
  273.   *p++ = 0;     // Class
  274.  
  275.   /* copy the title of the dialog, null terminate the string. */
  276.   nchar = wsprintf (p, TEXT("Title 2"));
  277.   p += nchar;
  278.   *p++ = 0;
  279.  
  280.   /* add in the wPointSize and szFontName here iff the DS_SETFONT bit on */
  281.   *p++ = 24;
  282.   nchar = wsprintf (p, TEXT("Courier"));
  283.   p += nchar;
  284.   *p++ = 0;
  285.  
  286.  
  287.   /* make sure the first item starts on a DWORD boundary */
  288.   { ULONG l;
  289.  
  290.   l = (ULONG) p;
  291.   l +=3;
  292.   l >>=2;
  293.   l <<=2;
  294.   p = (PWORD) l;
  295.   }
  296.  
  297.  
  298.   /* now start with the first item */
  299.   lStyle              = BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP;
  300.   x                   = 10;
  301.   y                   = 60;
  302.   cx                  = 80;
  303.   cy                  = 20;
  304.   wId                 = IDOK;
  305.  
  306.  
  307.   *p++ = LOWORD (lStyle);
  308.   *p++ = HIWORD (lStyle);
  309.   *p++ = LOWORD (lExtendedStyle);
  310.   *p++ = HIWORD (lExtendedStyle);
  311.   *p++ = x ;
  312.   *p++ = y ;
  313.   *p++ = cx;
  314.   *p++ = cy;
  315.   *p++ = wId;
  316.  
  317.  
  318.   /* fill in class i.d., this time by name */
  319.   nchar = wsprintf (p, TEXT("BUTTON"));
  320.   p += nchar;
  321.   *p++ = 0;
  322.  
  323.  
  324.   /* copy the text of the first item, null terminate the string. */
  325.   nchar = wsprintf (p, TEXT("OK"));
  326.   p += nchar;
  327.   *p++ = 0;
  328.  
  329.  
  330.   /* make sure the second item starts on a DWORD boundary */
  331.   { ULONG l;
  332.  
  333.   l = (ULONG) p;
  334.   l +=3;
  335.   l >>=2;
  336.   l <<=2;
  337.   p = (PWORD) l;
  338.   }
  339.  
  340.  
  341.   lStyle              = ES_CENTER | ES_MULTILINE | WS_BORDER | WS_VISIBLE | WS_CHILD | WS_TABSTOP;
  342.   x                   = 10;
  343.   y                   = 20;
  344.   cx                  = 80;
  345.   cy                  = 20;
  346.   wId                 = 57;
  347.  
  348.  
  349.   *p++ = LOWORD (lStyle);
  350.   *p++ = HIWORD (lStyle);
  351.   *p++ = LOWORD (lExtendedStyle);
  352.   *p++ = HIWORD (lExtendedStyle);
  353.   *p++ = x ;
  354.   *p++ = y ;
  355.   *p++ = cx;
  356.   *p++ = cy;
  357.   *p++ = wId;
  358.  
  359.   /* fill in class i.d., this time by name */
  360.   nchar = wsprintf (p, TEXT("EDIT"));
  361.   p += nchar;
  362.   *p++ = 0;
  363.  
  364.   /* copy the text of the second item, null terminate the string. */
  365.   nchar = wsprintf (p, TEXT("Edit string"));
  366.   p += nchar;
  367.   *p++ = 0;
  368.  
  369.  
  370.  
  371.  
  372.   CreateDialogIndirect (hInstance, (LPDLGTEMPLATE) pdlgtemplate, hwnd, (DLGPROC) About);
  373.  
  374.   LocalFree (LocalHandle (pdlgtemplate));
  375.  
  376. }
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.     /* Loop getting messages and dispatching them. */
  384.     while (GetMessage(&msg,NULL, NULL, NULL)) {
  385.       TranslateMessage(&msg);
  386.       DispatchMessage(&msg);
  387.     }
  388.  
  389.     return (msg.wParam);
  390. }
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401. /***************************************************************************\
  402. *    FUNCTION: MainWndProc
  403. \***************************************************************************/
  404. LRESULT APIENTRY MainWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  405. {
  406.   switch (message) {
  407.  
  408.  
  409.     case WM_PAINT: {
  410.       HDC hdc;
  411.       PAINTSTRUCT ps;
  412.  
  413.       hdc = BeginPaint(hwnd, &ps);
  414.       EndPaint (hwnd, &ps);
  415.  
  416.     } return FALSE;
  417.  
  418.  
  419.     case WM_DESTROY:
  420.       PostQuitMessage(0);
  421.       break;
  422.  
  423.     default:
  424.       break;
  425.  
  426.     } /* end switch */
  427.  
  428.     return (DefWindowProc(hwnd, message, wParam, lParam));
  429. }
  430.  
  431.  
  432.  
  433. /***************************************************************************\
  434. *    FUNCTION: About
  435. \***************************************************************************/
  436. LRESULT APIENTRY About(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  437. {
  438.   if ((message == WM_COMMAND) && (LOWORD(wParam) == IDOK)) {
  439.     EndDialog (hwnd, TRUE);
  440.     return TRUE;
  441.   }
  442.   if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE)) {
  443.     EndDialog (hwnd, TRUE);
  444.     return TRUE;
  445.   }
  446.   return FALSE;
  447. }
  448.